const { getToken, autoLogin } = require('./user.js')

const checkAuth = async () => {
  const token = getToken()
  if (!token) {
    // 尝试自动登录
    const loginSuccess = await autoLogin()
    if (!loginSuccess) {
      wx.redirectTo({
        url: '/pages/login/login',
        animationType: 'slide-in-right',
        duration: 300
      })
      return false
    }
  }
  return true
}

// 处理token过期
const handleTokenExpired = () => {
  wx.showToast({
    title: '登录已过期，请重新登录',
    icon: 'none',
    duration: 2000
  })
  
  setTimeout(() => {
    wx.redirectTo({
      url: '/pages/login/login',
      animationType: 'slide-in-right',
      duration: 300
    })
  }, 2000)
}

module.exports = {
  checkAuth,
  handleTokenExpired
} 